﻿using System;
using System.Web;
using System.Xml;
using System.Text.RegularExpressions;

namespace CNVP.Config
{
    public class ActivationCode
    {
        /// <summary>
        /// 封装的XML配置文件操作对象
        /// </summary>
        private static BaseXmlCtrl xml = new BaseXmlCtrl();

        /// <summary>
        /// 配置文件名称
        /// </summary>
        private static String xmlFileName = "Lisence";

        /// <summary>
        /// 读取指定项目下的值
        /// </summary>
        /// <param name="itemName">项目名</param>
        /// <returns>值</returns>
        public static String GetActivationCode()
        {
            xml.XmlPath = xmlFileName;
            return xml.GetSingle("root/ActivationCode");
        }
        /// <summary>
        /// 保存配置信息
        /// </summary>
        /// <param name="strValue">标签值</param>
        public static void SaveActivationCode( String strValue)
        {
            xml.XmlPath = xmlFileName;
            xml.SaveXmlConfig("ActivationCode", strValue);
        }

        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <param name="Target"></param>
        /// <returns></returns>
        public static string GetConfigValue(String activationCode,String target)
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(activationCode);
            XmlElement root = xdoc.DocumentElement;
            XmlNodeList elemList = root.GetElementsByTagName(target);
            try
            {
                return elemList[0].InnerText;
            }
            catch
            {
                return null;
            }
        }

        #region 数字字典
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Config">输入的字符串</param>
        /// <param name="Product">产品标识</param>
        /// <param name="paramName">参数名</param>
        /// <returns></returns>
        public static string GetParamValue(string Config, string Product, string paramName)
        {
            string[] configString = Config.Split(new char[] { ';' });
            string getString = String.Empty;
            foreach (string g in configString)
            {
                if (Regex.IsMatch(g, @"Product=" + Product, RegexOptions.IgnoreCase))
                {
                    getString = g;
                    break;
                }
            }
            Regex c = new Regex(@"\{((?<paramName>\w+?)=(?<paramValue>\w+?),?)+\}", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            string s = String.Empty;
            foreach (Match m in c.Matches(getString))
            {
                for (int i = 0; i < m.Groups["paramName"].Captures.Count; i++)
                {
                    if (m.Groups["paramName"].Captures[i].Value == paramName)
                    {
                        return m.Groups["paramValue"].Captures[i].Value;
                    }
                }
            }
            return s;
        }
        #endregion

    }
}
